package com.mcmaster.android.control;

import java.io.FileNotFoundException;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.mcmaster.android.MapOverlay;
import com.mcmaster.android.Output;
import com.mcmaster.android.data.Building;
import com.mcmaster.android.data.BuildingDatabase;
import com.mcmaster.android.data.Tour;
import com.mcmaster.android.data.TourDatabase;
import com.mcmaster.android.data.TourExplorer;
import com.mcmaster.android.menus.GuidedMenu;
import com.mcmaster.android.menus.GuidedToursMenu;

public class GuidedTourControl implements QRControl, Controller {
	
	
	private Activity ui_;
	private MapController map_controller_;
	private MapView map_view_;
	private List<Overlay> list_overlays_;
	private TourExplorer current_tour_ = new TourExplorer();
	
	public GuidedTourControl(Activity u, MapView mv, Tour t) {
		ui_				 	= u;
		map_controller_ 	= mv.getController();
		list_overlays_ 		= mv.getOverlays();
		map_view_ 			= mv;
		
		map_controller_.setZoom(19);
		current_tour_.setTour(t);
		
		Building current_building = BuildingDatabase.getBuildingFromQR(current_tour_.current());
		addPointOverlay(current_building.getLocation());	
	}
	
	private void addPointOverlay(GeoPoint point) {
		MapOverlay mo_ = new MapOverlay(point);
		
		map_controller_.animateTo(point);		
		list_overlays_.add(mo_);
		map_view_.invalidate();
	}
	
	// Todo: Add Actual QR Part, separate funcs
	public void startQRReader() {
		String tempqr_				= "f7f34f9f2jf92f4324324322f29fjk290fj";
		String current_qr			= current_tour_.current();
		Building current_building_ 	= BuildingDatabase.getBuildingFromQR(current_qr);
		
		if (tempqr_.equals(current_building_.getQR())) {
			map_controller_.setZoom(19);
			
			addPointOverlay(current_building_.getLocation());
			current_tour_.next();
			
		}
		else {
			Toast.makeText(ui_, "Incorrect building scanned.", Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public void openBuildingInfo(Building b) {
		Intent i = new Intent(ui_, Output.class);
		
		i.putExtra("name", b.getName());
		i.putExtra("descrip", b.getDescription());
		ui_.startActivity(i);
	}

}
